<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_calon_siswa extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function get_data($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'no_pendaftaran';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		//$kd_pt = isset($_POST['kd_pt']) ? strval($_POST['kd_pt']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.*, b.nama_psb, c.nama_kelompok');
			$this->db->from('tbl_sd_calonsiswa a');
			$this->db->join('tbl_sd_psb b', 'b.id_psb=a.id_psb');
			$this->db->join('tbl_sd_psb_kelompok c', 'c.id_kelompok=a.id_kelompok');
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			if($page != 0)
				$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function get_data_edit($id){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('tbl_sd_calonsiswa');
		$this->db->where('id', $id);
		
		return $this->db->get();
	}
	
	function togrid($data, $count){
		$response->total = $count;
		$response->rows = array();
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		
		return json_encode($response);
	}
	
	function no_pendaftaran_exist($no_pendaftaran, $id_psb){
		$this->db->flush_cache();
		$this->db->where('no_pendaftaran', $no_pendaftaran);
		$this->db->where('id_psb', $id_psb);
		$res = $this->db->get('tbl_sd_calonsiswa');
		
		return ($res->num_rows() > 0);
	}
	
	function insert($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('no_pendaftaran', $data['no_pendaftaran']);
        $this->db->set('id_psb', $data['id_psb']);
        $this->db->set('id_kelompok', $data['id_kelompok']);
        $this->db->set('tahun_masuk', $data['tahun_masuk']);
        $this->db->set('nama', $data['nama']);
        $this->db->set('jk', $data['jk']);
        $this->db->set('tmp_lahir', $data['tmp_lahir']);
        $this->db->set('tgl_lahir', FormatDateToMysql($data['tgl_lahir']));
        $this->db->set('Agama', $data['Agama']);
        $this->db->set('kewarganegaraan', $data['kewarganegaraan']);
        $this->db->set('anakke', $data['anakke']);
        $this->db->set('jml_saudara', $data['jml_saudara']);
        $this->db->set('bahasa', $data['bahasa']);
        $this->db->set('foto', $data['foto']);
        $this->db->set('alamat', $data['alamat']);
        $this->db->set('tlp', $data['tlp']);
        $this->db->set('asal_sekolah', $data['asal_sekolah']);
        $this->db->set('gol_darah', $data['gol_darah']);
        $this->db->set('riwayat_penyakit', $data['riwayat_penyakit']);
        $this->db->set('nama_ayah', $data['nama_ayah']);
        $this->db->set('nama_ibu', $data['nama_ibu']);
        $this->db->set('status_ayah', $data['status_ayah']);
        $this->db->set('status_ibu', $data['status_ibu']);
        $this->db->set('alm_ayah', isset($data['alm_ayah'])?'1':'0');
        $this->db->set('alm_ibu', isset($data['alm_ibu'])?'1':'0');
        $this->db->set('tmp_lahir_ayah', $data['tmp_lahir_ayah']);
        $this->db->set('tmp_lahir_ibu', $data['tmp_lahir_ibu']);
        $this->db->set('tgl_lahir_ayah', $data['tgl_lahir_ayah']);
        $this->db->set('tgl_lahir_ibu', $data['tgl_lahir_ibu']);
        $this->db->set('pendidikan_ayah', $data['pendidikan_ayah']);
        $this->db->set('pendidikan_ibu', $data['pendidikan_ibu']);
        $this->db->set('pekerjaan_ayah', $data['pekerjaan_ayah']);
        $this->db->set('pekerjaan_ibu', $data['pekerjaan_ibu']);
        $this->db->set('penghasilan_ayah', $data['penghasilan_ayah']);
        $this->db->set('penghasilan_ibu', $data['penghasilan_ibu']);
        $this->db->set('nama_wali', $data['nama_wali']);
        $this->db->set('alamat_ortu', $data['alamat_ortu']);
        $this->db->set('tlp_ortu', $data['tlp_ortu']);
        $this->db->set('status', '1');
		
		$result = $this->db->insert('tbl_sd_calonsiswa');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function update($data){
		
		$this->db->flush_cache();
        $this->db->set('no_pendaftaran', $data['no_pendaftaran']);
        $this->db->set('id_psb', $data['id_psb']);
        $this->db->set('id_kelompok', $data['id_kelompok']);
        $this->db->set('tahun_masuk', $data['tahun_masuk']);
        $this->db->set('nama', $data['nama']);
        $this->db->set('jk', $data['jk']);
        $this->db->set('tmp_lahir', $data['tmp_lahir']);
        $this->db->set('tgl_lahir', FormatDateToMysql($data['tgl_lahir']));
        $this->db->set('Agama', $data['Agama']);
        $this->db->set('kewarganegaraan', $data['kewarganegaraan']);
        $this->db->set('anakke', $data['anakke']);
        $this->db->set('jml_saudara', $data['jml_saudara']);
        $this->db->set('bahasa', $data['bahasa']);
        $this->db->set('foto', $data['foto']);
        $this->db->set('alamat', $data['alamat']);
        $this->db->set('tlp', $data['tlp']);
        $this->db->set('asal_sekolah', $data['asal_sekolah']);
        $this->db->set('gol_darah', $data['gol_darah']);
        $this->db->set('riwayat_penyakit', $data['riwayat_penyakit']);
        $this->db->set('nama_ayah', $data['nama_ayah']);
        $this->db->set('nama_ibu', $data['nama_ibu']);
        $this->db->set('status_ayah', $data['status_ayah']);
        $this->db->set('status_ibu', $data['status_ibu']);
        $this->db->set('alm_ayah', isset($data['alm_ayah'])?'1':'0');
        $this->db->set('alm_ibu', isset($data['alm_ibu'])?'1':'0');
        $this->db->set('tmp_lahir_ayah', $data['tmp_lahir_ayah']);
        $this->db->set('tmp_lahir_ibu', $data['tmp_lahir_ibu']);
        $this->db->set('tgl_lahir_ayah', $data['tgl_lahir_ayah']);
        $this->db->set('tgl_lahir_ibu', $data['tgl_lahir_ibu']);
        $this->db->set('pendidikan_ayah', $data['pendidikan_ayah']);
        $this->db->set('pendidikan_ibu', $data['pendidikan_ibu']);
        $this->db->set('pekerjaan_ayah', $data['pekerjaan_ayah']);
        $this->db->set('pekerjaan_ibu', $data['pekerjaan_ibu']);
        $this->db->set('penghasilan_ayah', $data['penghasilan_ayah']);
        $this->db->set('penghasilan_ibu', $data['penghasilan_ibu']);
        $this->db->set('nama_wali', $data['nama_wali']);
        $this->db->set('alamat_ortu', $data['alamat_ortu']);
        $this->db->set('tlp_ortu', $data['tlp_ortu']);
		
        $this->db->where('id', $data['id']);
        //$this->db->set('status', '1');
		
		$result = $this->db->update('tbl_sd_calonsiswa');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function delete($kode){
		$this->db->flush_cache();
		
		$this->db->where('id', $kode);
		$result = $this->db->delete('tbl_sd_calonsiswa');
	   
		//return
		if($result) {
				return TRUE;
		}else {
				return FALSE;
		}
	}
	
}

?>